//
//  ZZSegView.m
//  ZuZuBao
//
//  Created by Felix on 2024/4/10.
//

#import "ZZSegView.h"

@interface ZZSegView ()

@property (nonatomic, strong) NSArray * array;
@property (nonatomic, strong) UIView * lineView;

@end

@implementation ZZSegView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.lineView = [[UIView alloc] init];
        self.lineView.layer.cornerRadius = 2;
        self.lineView.layer.masksToBounds = YES;
        self.lineView.backgroundColor = kRGBAColor(0x1457FF, 1);
        [self addSubview:self.lineView];
    }
    return self;
}

-(void)setZZSegWithArray:(NSArray *)array
{
    CGFloat width = self.frame.size.width / array.count;
    for (int i = 0; i < array.count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width * i, 0, width, self.frame.size.height);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.tag = 1000 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        
        if (i == 0) {
            self.lineView.frame = CGRectMake(button.center.x - 11, self.frame.size.height - 4, 22, 4);
        }
    }
}

-(void)buttonAction:(UIButton *)button {
    self.lineView.frame = CGRectMake(button.center.x - 11, self.frame.size.height - 4, 22, 4);
    // 代理 点击事件
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickToSeg:)]) {
        [self.delegate clickToSeg:button.tag - 1000];
    }
}

-(void)setSelectIndex:(NSInteger)index {
    UIButton * button = [self viewWithTag:1000 + index];
    self.lineView.frame = CGRectMake(button.center.x - 11, self.frame.size.height - 4, 22, 4);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
